﻿using System;
using System.Xml.Linq;
using IcicleFramework.Components.Actions;
using Microsoft.Xna.Framework;

namespace IcicleFramework.GameServices
{
    public class ActionFactory : IActionFactory
    {
        public void Update(GameTime gameTime) {}

        public void Initialize() {}

        public IAction GenerateAction(XElement element)
        {
            XAttribute classAttrib = element.Attribute("class"),
                         typeAttrib = element.Attribute("type");

            IAction action = null;

            if (classAttrib != null && typeAttrib != null)
            {
                string assembly;

                if (element.Attribute("assembly") != null)
                    assembly = element.Attribute("assembly").Value;
                else
                {
                    string[] temp = classAttrib.Value.Split('.');
                    assembly = temp[0];
                }

                Type classType = Type.GetType(String.Format("{0},{1}", classAttrib.Value, assembly));

                action = (IAction)Activator.CreateInstance(classType);
                action.Deserialize(element);
            }

            return action;
        }
    }
}
